<template>
    <div class="login">
        <div class="top">
            <img :src="logo_path" alt="/">
        </div>
        <div class="main">
            <el-form :model="loginObj" :rules="rules" ref="login">
                <el-form-item>
                    <h1>万和超市后台管理系统 1.0</h1>
                </el-form-item>
                <el-form-item prop="username">
                    <el-input v-model="loginObj.username" prefix-icon="el-icon-user" placeholder="请输入账号"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="loginObj.password" prefix-icon="el-icon-lock" type="password"
                        placeholder="请输入密码"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button @click="loginFn" type="success" round>登 录</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button @click="clearlogin" type="info" plain round>重 置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import { postLoginApi } from '@/api/usersapi/index.js'
export default {
    data() {
        return {
            // 如果想要让src支持本地的图片或者文件，必须吧地址通过require函数包起来
            logo_path: require('@/assets/imgs/logo.png'),
            loginObj: {
                username: 'admin',
                password: '123456',
            },
            rules: {
                username: [
                    { required: true, message: '用户名必填' },
                    { max: 10, min: 5, message: '用户名必须在5~10位之间' }
                ],
                password: [
                    { required: true, message: '用户名必填' },
                    { max: 10, min: 5, message: '用户名必须在5~10位之间' }
                ]
            }
        }
    },
    methods: {
        loginFn() {
            this.$refs.login.validate(async valid => {
                if (valid) {
                    const res = await postLoginApi(this.loginObj)
                    if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
                    else {
                        // 登录成功
                        this.$message.success(res.meta.msg)
                        // 进行本地存储
                        localStorage.setItem('userinfo', JSON.stringify(res.data))
                        this.$router.push('/Layout/welcome')
                    }
                }
            })
        },
        clearlogin() {
            this.loginObj.username = ''
            this.loginObj.password = ''
        }
    },
    created() {
        // 通过问号拼接的参数来获取当前路由的query参数
        let msg = this.$route.query.msg
        if (msg) this.$message.warning(msg);
    },
}
</script>

<style lang="scss" scoped>
@import '../../assets/CSS/common.css';
@import '@/assets/scss/common.scss';

.login {
    width: 100vw;
    height: 100vh;
    @include flex();
    flex-direction: column;

    .top {
        width: 100%;
        height: 150px;
        @include flex();

        img {
            width: 200px;
        }
    }

    .main {
        width: 100%;
        flex: 1;
        background-color: $main_color;
        @include flex();

        .el-form {
            width: 500px;
            background-color: #fff;
            box-sizing: border-box;
            padding: 40px 30px;
            border-radius: 20px;

            .el-form-item {
                padding: 10px;
            }

            .el-button {
                width: 100%;
            }

            h1 {
                color: $main_color;
                font-weight: hold;
                text-align: center;
            }
        }
    }
}
</style>